﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Collections;

namespace GUI.TKKeToan // VƯƠNG TRÍ LỢI
{
    public partial class frmQuanLy : Form
    {

        #region Cap nhap giao diện main
        public delegate void GetBoolean(Boolean capnhat);
        public GetBoolean GetData;
        #endregion

        #region Delegate form xử lý
        public void GetValue(Boolean capnhat)
        {
            if (capnhat == true)
            {
                HienThi();
                if (GUI.TKKeToan.frmXuLy.CheckClose == true)
                {
                    Them();
                }
                GUI.TKKeToan.frmXuLy.CheckClose = false;
                
            }
        }
        #endregion

        #region CheckPerMission
        private Boolean CheckPerMission(int active)
        {
            return Common.Utilities.Check(Common.FormName.Quanlytaikhoanketoan, active);
        }
        #endregion

        #region Khoi Tao
        private string MaTkkt = string.Empty;
        private Entities.TKKeToan taikhoanketoan;
        private Boolean kiemtra = false;
        private Entities.TKKeToan[] DanhSach;
        private bool CheckMa = true;
        private bool CheckTen = false;
        private Boolean CheckClose = false;
        public frmQuanLy()
        {
            InitializeComponent();
            Event();
        }
        #endregion

        #region Event
        private void Event()
        {
            dgvHienThi.KeyDown += new KeyEventHandler(dgvHienThi_KeyDown);
            rdbTimMa.Checked = true;
        }

        void dgvHienThi_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                this.Sua();
            }
        }

        private void frmQuanLy_FormClosing(object sender, FormClosingEventArgs e)
        {
            try
            {
                if (CheckClose == false)
                {
                    System.Windows.Forms.DialogResult ketqua = System.Windows.Forms.MessageBox.Show("Bạn có chắc chắn muốn thoát không ?", "Thông báo", System.Windows.Forms.MessageBoxButtons.YesNo);
                    {
                        if (ketqua == System.Windows.Forms.DialogResult.Yes)
                        {
                            e.Cancel = false;
                        }
                        else
                        {
                            e.Cancel = true;
                        }
                    }
                }
                GetData(true);
            }
            catch (Exception)
            { }
        }

        private void rdbTimTen_CheckedChanged(object sender, EventArgs e)
        {
            try
            {
                CheckTen = (bool)rdbTimTen.Checked;
                if (CheckTen == true)
                {
                    CheckMa = false;
                }
                else
                { CheckMa = true; }
            }
            catch (Exception ex)
            { string s = ex.Message; }
        }

        private void rdbTimMa_CheckedChanged(object sender, EventArgs e)
        {
            try
            {
                CheckMa = (bool)rdbTimMa.Checked;
                if (CheckMa == false)
                {
                    CheckTen = true;
                }
                else
                { CheckTen = false; }
            }
            catch (Exception ex)
            { string s = ex.Message; }
        }

        private void txtTimKiem_TextChanged(object sender, EventArgs e)    
         {
            try
            {
                SearchData(this.CheckTen, this.CheckMa, dgvHienThi, txtTimKiem.Text, this.DanhSach);
            }
            catch (Exception ex)
            {
                string s = ex.Message;
                Entities.TKKeToan[] k = new Entities.TKKeToan[0];
                dgvHienThi.DataSource = k;
            }
        }

        private void dgvHienThi_DoubleClick(object sender, EventArgs e)
        {
            this.Sua();
        }

        private void dgvHienThi_SelectionChanged(object sender, EventArgs e)
        {
            if (dgvHienThi.Rows.Count <= 0)
            { return; }
            //lay ma nhan vien tren gidview
            MaTkkt = dgvHienThi.CurrentRow.Cells["MaTKKeToan"].Value.ToString();
            this.GetRow();

        }

        private void GetRow()
        {
            try
            {
                if (DanhSach == null) { return; }
                for (int i = 0; i < this.DanhSach.Length; i++)
                {
                    if (DanhSach[i].MaTKKeToan == this.MaTkkt)
                    {
                        taikhoanketoan = DanhSach[i];
                        break;
                    }
                    else
                    { continue; }
                }
            }
            catch (Exception ex)
            {
                string s = ex.Message;
            }
        }

        private void btnThem_Click(object sender, EventArgs e)
        {
            this.Them();
        }

        private void btnSua_Click(object sender, EventArgs e)
        {
            this.Sua();
        }

        private void btnXoa_Click(object sender, EventArgs e)
        {
            this.Xoa();
        }

        private void btnThoat_Click(object sender, EventArgs e)
        {
            this.Thoat();
        }

        private void frmQuanLy_Load(object sender, EventArgs e)
        {
            try
            {
                HienThi();
                this.WindowState = FormWindowState.Maximized;
                MaTkkt = dgvHienThi.CurrentRow.Cells["MaTKKeToan"].Value.ToString();
                this.GetRow();
                lblTenNhanVien.Text = UtilitiesVariable.User.TenDangNhap; 
            }
            catch 
            { }

        }

        private void frmQuanLy_KeyDown(object sender, KeyEventArgs e)
        {
          
        }

        #endregion

        #region XuLy

        #region hienthi
        private void HienThi()
        {
            Entities.TKKeToan tkkt = new Entities.TKKeToan();
            tkkt.HanhDong = "0";
            // Common.StaticVariable.InterfaceObject cham' 1 cai se lay dc cac ham tu sql ma minh da tao trong interface
            DanhSach = Common.StaticVariable.InterfaceObject.SelectTKKeToan(tkkt);
           
            dgvHienThi.DataSource = DanhSach;
            //hien thi lai cai gidview
            HienThiList();
        }
        private void HienThiList()
        {
            try
            {
                if (DanhSach == null)
                {
                    Entities.TKKeToan gitri = new Entities.TKKeToan("0");
                    dgvHienThi.DataSource = gitri;
                }
                for (int i = 0; i < dgvHienThi.ColumnCount; i++)
                {
                    dgvHienThi.Columns[i].Visible = false;
                }
                dgvHienThi.Columns["HanhDong"].HeaderText = "STT";
                dgvHienThi.Columns["HanhDong"].Visible = true;
                dgvHienThi.Columns["MaTKKeToan"].HeaderText = "Mã tk kế toán";
                dgvHienThi.Columns["MaTKKeToan"].Visible = true;
                dgvHienThi.Columns["TenTaiKhoan"].HeaderText = "Tên tk kế toán";
                dgvHienThi.Columns["TenTaiKhoan"].Visible = true;
                dgvHienThi.Columns["GhiChu"].HeaderText = "Ghi chú";
                dgvHienThi.Columns["GhiChu"].Visible = true;
                if (dgvHienThi.RowCount > 0)
                {
                    Common.Utilities.CountDatagridview(dgvHienThi);
                    dgvHienThi.Focus();
                    dgvHienThi.Select();
                }
                dgvHienThi.SelectionMode = DataGridViewSelectionMode.FullRowSelect;
                dgvHienThi.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.Fill;

            }
            catch
            { }
        }
        #endregion

        #region Them
        private void Them()
        {
            try
            {
                if (CheckPerMission(1) != true)
                {
                    MessageBox.Show(Common.MSG.MSG10(), Common.MSG.TieuDe, MessageBoxButtons.OK);
                    return;
                }

                    GUI.TKKeToan.frmXuLy frm = new GUI.TKKeToan.frmXuLy(Common.Constants.Insert, taikhoanketoan);
                    frm.GetData = new GUI.TKKeToan.frmXuLy.GetString(GetValue);
                    frm.Show();
                
            }
            catch (Exception ex)
            { string s = ex.Message; }
        }
        #endregion

        #region Sua
        private void Sua()
        {
            try
            {
                if (CheckPerMission(2) != true)
                {
                    MessageBox.Show(Common.MSG.MSG9(), Common.MSG.TieuDe,MessageBoxButtons.OK);
                    return;
                }
                if (DanhSach == null)
                {
                    MessageBox.Show(Common.MSG.MSG20(), Common.MSG.TieuDe);
                    return;
                }
                if (this.DanhSach.Length > 0)
                {
                    for (int i = 0; i < DanhSach.Length; i++)
                    {
                        if (DanhSach[i].MaTKKeToan == dgvHienThi.CurrentRow.Cells["MaTKKeToan"].Value.ToString())
                        {
                            taikhoanketoan = DanhSach[i];
                        }
                    }

                    if (taikhoanketoan.MaTKKeToan != null)
                    {

                        GUI.TKKeToan.frmXuLy frm = new GUI.TKKeToan.frmXuLy(Common.Constants.Update, this.taikhoanketoan);
                        frm.GetData = new GUI.TKKeToan.frmXuLy.GetString(GetValue);
                        frm.Show();

                    }
                    else
                    { MessageBox.Show(Common.MSG.MSG20(), Common.MSG.TieuDe); return; }
                }
                else {
                        MessageBox.Show(Common.MSG.MSG20(), Common.MSG.TieuDe);
                        return;
                }
            }
            catch
            {
            }
        }
        #endregion

        #region Tim Kiem
        private void SearchData(bool rdoMa, bool rdoTen, DataGridView dgv, string giatritim, Entities.TKKeToan[] mangtim)
        {
            try
            {
                if (giatritim != string.Empty)
                {
                    if (mangtim.Length > 0)
                    {
                        ArrayList list = new ArrayList();
                        Entities.TKKeToan[] tt1_search = null;
                        if (rdoTen == false)
                        {
                            if (mangtim == null)
                            {
                                Entities.TKKeToan[] lay = new Entities.TKKeToan[0];
                                dgv.DataSource = lay;
                            }
                            else
                            {
                                for (int i = 0; i < mangtim.Length; i++)
                                {
                                    int index = -1;
                                    index = mangtim[i].TenTaiKhoan.ToString().ToLower().IndexOf(giatritim.ToLower());
                                    if (index >= 0)
                                    {
                                        list.Add(mangtim[i]);
                                    }
                                }
                                if (list.Count <= 0)
                                {
                                    Entities.TKKeToan[] lay = new Entities.TKKeToan[0];
                                    dgv.DataSource = lay;
                                }
                                else
                                {
                                    int n = list.Count;
                                    if (n == 0) { tt1_search = null; }
                                    tt1_search = new Entities.TKKeToan[n];
                                    for (int i = 0; i < n; i++)
                                    {
                                        tt1_search[i] = (Entities.TKKeToan)list[i];
                                    }
                                    dgv.DataSource = tt1_search;
                                    list = null;
                                    tt1_search = null;
                                }
                            }
                        }
                        if (rdoMa == false)
                        {
                            if (mangtim == null)
                            {
                                Entities.TKKeToan[] lay = new Entities.TKKeToan[0];
                                dgv.DataSource = lay;
                            }
                            else
                            {
                                for (int i = 0; i < mangtim.Length; i++)
                                {
                                    int index = -1;
                                    index = mangtim[i].MaTKKeToan.ToString().ToLower().IndexOf(giatritim.ToLower());
                                    if (index >= 0)
                                    {
                                        list.Add(mangtim[i]);
                                    }
                                }
                                if (list.Count <= 0)
                                {
                                    Entities.TKKeToan[] lay = new Entities.TKKeToan[0];
                                    dgv.DataSource = lay;
                                }
                                else
                                {
                                    int n = list.Count;
                                    if (n == 0) { tt1_search = null; }
                                    tt1_search = new Entities.TKKeToan[n];
                                    for (int i = 0; i < n; i++)
                                    {
                                        tt1_search[i] = (Entities.TKKeToan)list[i];
                                    }
                                    dgv.DataSource = tt1_search;
                                    list = null;
                                    tt1_search = null;
                                }
                            }
                        }
                    }
                    else
                    {
                        Entities.TKKeToan[] k = new Entities.TKKeToan[0];
                        dgv.DataSource = k;
                    }

                }
                else
                {
                    dgv.DataSource = mangtim;
                }
                if (DanhSach.Length > 0)
                {
                    //dgvHienThi.SelectedIndex = 0;
                }
            }
            catch (Exception ex)
            {
                string s = ex.Message;
                Entities.TKKeToan[] k = new Entities.TKKeToan[0];
                dgv.DataSource = k;
            }
        }
        #endregion

        #region Xoa
        private void Xoa()
        {
            try
            {
                if (CheckPerMission(3) != true)
                {
                    MessageBox.Show(Common.MSG.MSG11(), Common.MSG.TieuDe, MessageBoxButtons.OK);
                    return;
                }
                // check xem da chon ban ghi chua
                if (DanhSach == null)
                {
                    MessageBox.Show(Common.MSG.MSG20(), Common.MSG.TieuDe);
                    return;
                }

                if (this.DanhSach.Length > 0)
                {
                    Entities.CheckRefer cr = new Entities.CheckRefer();
                    cr.MaTruong = this.taikhoanketoan.MaTKKeToan;
                    cr.TenTruong = "TaiKhoanKeToan";
                    if (!Common.StaticVariable.InterfaceObject.CheckRefer(cr))
                    {
                        MessageBox.Show(cr.MaTruong + " đã có giao dịch không thể xóa", Common.MSG.TieuDe, MessageBoxButtons.OK);
                        return;
                    }

                    System.Windows.Forms.DialogResult giatri = System.Windows.Forms.MessageBox.Show(Common.MSG.MSG1(this.MaTkkt), Common.MSG.TieuDe, System.Windows.Forms.MessageBoxButtons.YesNo);
                    {
                        if (giatri == System.Windows.Forms.DialogResult.Yes)
                        {
                            int k = Common.StaticVariable.InterfaceObject.DeleteTKKeToan(taikhoanketoan);
                            if (k == 0)
                            {
                                HienThi();
                                this.MaTkkt = string.Empty;
                                MaTkkt = dgvHienThi.CurrentRow.Cells["MaTKKeToan"].Value.ToString();
                                this.GetRow();
                            }
                            else
                            { MessageBox.Show("Xóa Thất bại", Common.MSG.TieuDe,MessageBoxButtons.OK); }
                        }
                    }
                }
                else
                {
                    MessageBox.Show(Common.MSG.MSG20(), Common.MSG.TieuDe);
                    return;
                }
            }
            catch (Exception ex)
            { string s = ex.Message; }
        }
        #endregion

        #region Thoat
        private void Thoat()
        {
            try
            {
                System.Windows.Forms.DialogResult ketqua = System.Windows.Forms.MessageBox.Show("Bạn có chắc chắn muốn thoát không ?", "Thông báo", System.Windows.Forms.MessageBoxButtons.YesNo);
                {
                    if (ketqua == System.Windows.Forms.DialogResult.Yes)
                    {
                        this.CheckClose = true;
                        this.Close();
                    }
                }
            }
            catch (Exception)
            { }
        }
        #endregion

        #endregion

    }
}
